package com.xes.apps.ams.scheduler.service;

import javax.inject.Inject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import com.google.common.collect.Multimap;
import com.xes.ams.client.api.AttendVo;
import com.xes.ams.client.api.AttendResult;
import com.xes.ams.client.api.BaseException;
import com.xes.apps.ams.client.core.service.AttendDataService;
import com.xes.apps.ams.config.ServerConfig;
import com.xes.apps.ams.scheduler.Scheduler;

/***
 * @description 下载预考勤数据调度任务
 * @author wjl
 * @ClassName: DownloadAttendSchedulerService
 * @Version 版本
 * @ModifiedBy wjl
 * @Copyright ers
 * @date 2015年11月26日 上午11:46:05
 */
@Service
public class DownloadAttendByTodaySchedulerService implements Scheduler {
	private static final Logger log = LogManager.getLogger(DownloadAttendByTodaySchedulerService.class);
	@Inject
	private ServerConfig config;
	@Inject
	private AttendDataService attendDataService;

	@Override
	@Scheduled(fixedDelay = 1000 * 60 * 5)	// 5 minuts
	public void execute() {
		if (config.getIsDownloadAttendByToday() == true) {
			return;
		}
		StopWatch stopWatch = new StopWatch("预考勤数据下载服务VenueId：" + config.getVenueId());
		stopWatch.start("下载");
		log.info("预考勤数据下载开始");
		AttendResult attendResult = null;
		try {
			attendResult = attendDataService.downloadAttendDataByToday(config.getVenueId());
		} catch (BaseException e) {
			log.error("预考勤数据下载失败:{}:{}", e.getResultCode(), e.getMessage());
		}
		log.info("预考勤数据今日共计下载:{}条", attendResult.getDataCount());
		stopWatch.stop();
		log.info(stopWatch.prettyPrint());
		if (attendResult.getDataCount() > 0) {
			config.setIsDownloadAttendByToday(true);
		}
	}

}
